package com.company;

import javax.swing.*;
import java.awt.*;


public class Gui {

    public static JFrame jf = new JFrame("GameAnt");
    public static JLabel l1 = new JLabel("Game Ant");
    public static JLabel l2 = new JLabel("Directions:");
    public static JLabel l3 = new JLabel("Result: ");
    public static JLabel l4 = new JLabel("Finish-time: ");
    public static JLabel l5 = new JLabel("Current-direction: ");
    public static JLabel l6 = new JLabel("MAX: ");
    public static JLabel l7 = new JLabel("MIN: ");

    public static JTextField textField = new JTextField(8);
    public static JButton btn = new JButton("Submit");
    public static JButton btn2 = new JButton("Next Move");
    public static int directions = 0;
    public Ant[] ant = new Ant[CreepingGame.antNum];
    public JPanel p = new JPanel() {
        @Override
        public void paint(Graphics g) {
            g.drawRect(50, 400, 900, 10);
            g.setColor(Color.red);
            g.fillOval(140, 400, 10, 10);
            g.setColor(Color.ORANGE);
            g.fillOval(290, 400, 10, 10);
            g.setColor(Color.blue);
            g.fillOval(380, 400, 10, 10);
            g.setColor(Color.magenta);
            g.fillOval(530, 400, 10, 10);
            g.setColor(Color.black);
            g.fillOval(800, 400, 10, 10);
        }
    };

    public Gui() {
        jf.setSize(1000, 600);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        l1.setFont(new Font(null, Font.PLAIN, 40));
        l1.setBounds(350, 50, 400, 100);
        jf.add(l1);

        l2.setFont(new Font(null, Font.PLAIN, 20));
        l2.setBounds(230, 150, 100, 30);
        jf.add(l2);

        l3.setBounds(230, 230, 250, 30);
        l3.setFont(new Font(null, Font.PLAIN, 20));
        jf.add(l3);

        l4.setBounds(230, 270, 250, 30);
        l4.setFont(new Font(null, Font.PLAIN, 20));
        jf.add(l4);

        l5.setBounds(230, 310, 250, 30);
        l5.setFont(new Font(null, Font.PLAIN, 20));
        jf.add(l5);

        l6.setBounds(230, 350, 100, 30);
        l6.setFont(new Font(null, Font.PLAIN, 20));
        jf.add(l6);

        l7.setBounds(350, 350, 100, 30);
        l7.setFont(new Font(null, Font.PLAIN, 20));
        jf.add(l7);

        textField.setFont(new Font(null, Font.PLAIN, 20));
        textField.setBounds(350, 150, 100, 30);
        jf.add(textField);

        btn.setFont(new Font(null, Font.PLAIN, 20));
        btn.setBounds(470, 150, 150, 30);
        jf.add(btn);

        btn2.setFont(new Font(null, Font.PLAIN, 20));
        btn2.setBounds(470, 190, 150, 30);
        jf.add(btn2);

        jf.add(p);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        jf.remove(p);

        btn.addActionListener(e -> {
            int tmp = 0;
            String[] str = (textField.getText()).split(" ");
            for (int i = 0; i < str.length; i++) {
                tmp += Integer.parseInt(str[i]) * Math.pow(2, i);
            }
            directions = tmp;
            PlayRoom playRoom = new PlayRoom(ant);
            playRoom.buildDirections(ant, directions);
            printDirection();
            playRoom.start();
            directions++;
        });

        btn2.addActionListener(e -> {
            PlayRoom playRoom = new PlayRoom(ant);
            playRoom.buildDirections(ant, directions);
            printDirection();
            playRoom.start();
            directions++;
        });
        jf.setVisible(true);
    }

    public void printDirection() {
        StringBuilder direction = new StringBuilder("Current-direction: ");
        for (Ant a : ant) {
            direction.append(a.getDirection());
            direction.append(" ");
        }
        l5.setText(direction.toString());
    }

    public static void main(String[] args) {
        new Gui();
    }
}